package logic

import (
	"context"

	"user_rpc/internal/svc"
	"user_rpc/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserByPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserByPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByPasswordLogic {
	return &GetUserByPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserByPasswordLogic) GetUserByPassword(in *user.UserInfo) (*user.UserInfo, error) {
	u, err := l.svcCtx.UserModel.FindOneByPassword(l.ctx, in.Name, in.Password)
	if err != nil {
		l.Logger.Errorf("GetUserByPassword failed: name=%s, pass=%s, error: %v", in.Name, in.Password, err)
		return nil, err
	}
	return &user.UserInfo{Id: u.Id, Name: u.Name, Password: u.Password}, nil
}
